<?php

namespace app\wxapi\controller;

use think\Controller;
use Endroid\QrCode\QrCode;
use think\Request;
use think\Response;
use think\Session;
use EasyWeChat\Payment\Order;

class Index extends Api
{
    protected $beforeActionList = [];
    public function getIndex()
    {
        $payment = $this->app->payment;
        $attributes = [
            'trade_type'       => 'JSAPI', // JSAPI，NATIVE，APP...
            'body'             => 'iPad mini 16G 白色',
            'detail'           => 'iPad mini 16G 白色',
            'out_trade_no'     => '1217752501201407033233368018',
            'total_fee'        => 5388,
            'notify_url'       => 'http://wx.shsytour.cn', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'openid'           => 'oEnxIw_1wAGBb4ei73sQcsUt7JQg', // trade_type=JSAPI，此参数必传，用户在商户appid下的唯一标识，
            // ...
        ];
        $order = new Order($attributes);
        $result = $payment->prepare($order);
        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
            $prepayId = $result->prepay_id;
            halt($config = $payment->configForJSSDKPayment($prepayId));
        }
        exit;

        $response = $app->oauth->scopes(['snsapi_userinfo'])
                          ->redirect();
        $response->send();
        $userService = $app->user;
        $u = $userService->get('oEnxIw_1wAGBb4ei73sQcsUt7JQg');
        $broadcast = $app->broadcast;
        halt($u);
        // halt($broadcast->previewTextByName('shsytour.cn', 'niexiaojiang'));
        // halt($broadcast->sendText('hello', [$u->openid]));
        $tag = $app->user_tag; // $user['user_tag']
        //halt($tag->lists());
        halt($tag->usersOfTag(100));
    }
    public function getmenus()
    {
        $app = $this->app;
        $menu = $app->menu;
        $menus = $menu->all();
        //halt($menus);
        $matchRule = [
            "tag_id"             => "100"
        ];
        $buttons = [
            [
                "type" => "view",
                "name" => "双辉",
                "url"  => "http://www.shsytour.com/"
            ],
            [
                "name"       => "大事件",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "双辉上线测试",
                        "url"  => "http://www.shsytour.com/"
                    ],
                ],
            ],
            [
                "name"       => "服务中心",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "我的双辉",
                        "url" => "http://wx.shsytour.cn/user/index"
                    ],
                    [
                        "type" => "view",
                        "name" => "我的管理",
                        "url"  => "http://www.shsytour.com/"
                    ],
                    [
                        "type" => "scancode_waitmsg",
                        "name" => "我要入住",
                        "key" => "V1001_GOOD"
                    ],
                ],
            ],
        ];

        $menu->add($buttons, $matchRule);
        //$menus = $menu->test('oEnxIw_1wAGBb4ei73sQcsUt7JQg');
    }
    public function postIndex()
    {
        $broadcast = $this->app->broadcast;
        $server = $this->app->server;

        $server->setMessageHandler(function($message) use ($broadcast){
            // 注意，这里的 $message 不仅仅是用户发来的消息，也可能是事件
            // 当 $message->MsgType 为 event 时为事件
            \think\Log::record($message);
            if ($message->MsgType == 'event') {
                # code...
                switch ($message->Event) {
                    case 'subscribe':
                        return "您好！欢迎关注我!";
                        break;
                    case 'CLICK':
                        //$broadcast->previewText('尊敬的会员，你已经开启了免登陆模式。', $message->FromUserName);
                        break;
                    case 'location':
                        return ;
                        break;
                    default:
                        # code...
                        break;
                }
            }elseif ($message->MsgType == 'text') {
                return new \EasyWeChat\Message\Transfer();
            }
        });
        $response = $server->serve();
        $response->send();
        
    }
    public function getQrcode()
    {
        $qrcode = $this->app->qrcode;
        $result = $qrcode->temporary(1, 6 * 24 * 3600);
        //$result = $qrcode->forever(56);// 或者 $qrcode->forever("foo");
        $ticket = $result->ticket; // 或者 $result['ticket']
        $url = $qrcode->url($ticket);
        echo $result->url;
        echo $url;
    }
}
